package com.tour.silvertour.Service.impl;


import com.tour.silvertour.Common.Utils.R;
import com.tour.silvertour.Common.Utils.SecurityUtils;
import com.tour.silvertour.Entity.User;
import com.tour.silvertour.Mapper.UserMapper;
import com.tour.silvertour.Service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Title: IUserServiceImpl
 * @Author 苗桐菲
 * @Package com.tour.silvertour.Service.impl
 * @Date 2024/3/30 20:32
 */
@Service
public class IUserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User selectUserByUserName(String userName){

        return userMapper.selectUserByUserName(userName);
    }

    @Override
    public R register(User user) {
        //判断用户名和密码是否为空

        // 判断账号是否存在
        User existingUser = userMapper.selectUserByUserName(user.getUserName());
        if(existingUser != null) {
            return R.error(200, "该账号已被注册");
        }
        // 加密密码
        String password = SecurityUtils.encryptPassword(user.getPassword());
        User newUser = new User();
        newUser.setUserName(user.getUserName());
        newUser.setPassword(password);
        newUser.setEmail(user.getEmail());
        newUser.setNickName(user.getNickName());
        newUser.setPhoneNumber(user.getPhoneNumber());
        userMapper.save(newUser);
        return R.success("注册成功");
    }
}
